<template>
    <div class="btn-open">
        <el-button 
        class="btns"
        type="primary" 
        size="small" 
        plain 
        :loading="fileUploading"
        @click="openLocalFile"
        v-czc="['投标方案制作', '点击按钮' , '打开本地文件',0 ,'biddingfiletool-btn-openLocalFile']"
        >
            打开本地文件
        </el-button>

        <el-button 
        class="btns"
        type="primary" 
        size="small" 
        plain 
        @click="openPanFile"
        v-czc="['投标方案制作', '点击按钮' , '打开云盘文件',0 ,'biddingfiletool-btn-openPanFile']"
        >
            打开云盘文件
        </el-button>

        <upload-s
            class="upload"
            ref="upload"
            single
            upload-only
            filetype=".doc,.docx"
            :filesize="50"
            :fileguid="fileguid"
            :extra="{newtypecode: 'yunpan', typecode: 'qt'}"
            @success="submitForm"
            @load-start="fileUploading = true"
            @load-end="fileUploading = false"
        ></upload-s>

        <my-dialog 
        v-model="dialogVisible" 
        width="600px" 
        title="打开云盘文件"
        >
            <table-doc v-if="dialogVisible" @open="openHandler"></table-doc>
        </my-dialog>
    </div>
</template>

<script>
import TableDoc from '../pan/components/table-doc.vue';
import UploadS from './upload-s';

export default {
    components: {
        TableDoc,
        UploadS,
    },
    props: {
        editing: {
            type: Boolean,
            default: false,
        },
        editreadonly: {
            type: Boolean,
            default: false,
        },
    },
    data() {
        return {
            dialogVisible: false,

            fileguid: '',
            fileUploading: false,

            tableData: [],
        };
    },
    methods: {
        checkIsEditing(cb) {
            if (this.editing && !this.editreadonly) {
                // 有文档在编辑中
                showConfirm('此操作将关闭当前文件，关闭前请先保存当前文件的内容，若未保存直接关闭，该文档内容可能不是最新内容', 'warning', () => {
                    cb();
                });

                return;
            }

            cb();
        },
        openLocalFile() {
            this.$store.dispatch('userinfo/checkIsLogin', () => {
                this.checkIsEditing(() => {
                    this.openFileSel();
                });
            });
        },
        openPanFile() {
            this.$store.dispatch('userinfo/checkIsLogin', () => {
                this.checkIsEditing(() => {
                    this.dialogVisible = true;
                });
            });
        },
        openFileSel() {
            this.$refs.upload.elClick();
        },
        getFileguid(cb) {
            this.$get(`${this.$store.state.api.sysUrl}/upload/getfileguid.json`, data => {
                this.fileguid = data.fileguid;
                
                cb && cb();
            });
        },
        submitForm() {
            this.$post(`${this.$store.state.api.sysUrl}/file/bidding/plan/edit.json`, {
                typecode: 'qt',
                fileid: this.fileguid,
                fileguid: this.fileguid,
            }, data => {
                this.getFileguid();

                this.openHandler(data);
            });
        },
        openHandler(data) {
            this.$emit('open', data);
            this.$emit('reload', data);

            this.dialogVisible = false;
        },
    },
    mounted() {
        this.getFileguid();
    },
};
</script>

<style scoped lang="scss">
.btn-open{
    display: inline-block;
    margin: 0 6px;
    position: relative;

    .btns{
        & + .btns{
            margin-left: 6px;
        }
    }

    .upload{
        width: 0;
        height: 0;
        overflow: hidden;
        position: absolute;
    }
}
</style>